<?php


if (isset($_GET['action']) && $_GET['action'] == "edit" && isset($_GET['category_id'])) {

	$category_id = $_GET['category_id'];

	$rp_category_item = $rp_category->get($category_id);

	$rp_category_item = $rp_category_item[0];

	$smarty->assign('rp_category_item', $rp_category_item);

	$smarty->assign('fileaction', "edit");

	$smarty->display('rp_category_edit.tpl', $_SESSION['uid']);

}elseif (isset($_GET['action']) && $_GET['action'] == "new") {

	$smarty->assign('fileaction', "new");

	$smarty->display('rp_category_edit.tpl', $_SESSION['uid']);

}elseif (isset($_GET['action']) && $_GET['action'] == "remove" && is_allowed(3))  {

	$category_id = $_GET['category_id'];

	$rp_category->remove($category_id);

	$rp_single_category = $rp_category->get();

	$smarty->assign('rp_category', $rp_single_category);

	$smarty->assign('message', "The rp category has been removed!");

	$smarty->display('rp_category_list.tpl', $_SESSION['uid']);

}elseif (isset($_POST['action']) && $_POST['action'] == "save") {

    $category_id = $_POST['category_id'];
    $category_short_name = $_POST['category_short_name'];
    $category_long_name = $_POST['category_long_name'];

	if($_POST['fileaction'] == "edit") {
		if($_FILES["picture"]['error'] == 4) {

			$rp_category_item = $rp_category->get($category_id);

			$rp_category_item = $rp_category_item[0];

			$picture_link = $rp_category_item['category_image'];

		}
	} else {
		    $picture_link = "";
	}

	if(!empty($_FILES["picture"])) {

		$downloaddir = $_SERVER['DOCUMENT_ROOT'] . "/images/rp_categories/";

		if(move_uploaded_file($_FILES["picture"]["tmp_name"], $downloaddir . $_FILES["picture"]["name"])) {

			$picture_link 		= $_FILES["picture"]["name"];

		} else {

		}
	}

	$rp_category->save($category_id, $category_short_name, $category_long_name, $picture_link);

	$smarty->assign('message', "The rp category has been changed/added!");

	$smarty->display('rp_category_edit.tpl', $_SESSION['uid']);

} else {

	$rp_category_item = $rp_category->get();

	$smarty->assign('rp_category', $rp_category_item);

	$smarty->display('rp_category_list.tpl', $_SESSION['uid']);
}





?>
